<?php

class ClassFinnet {
	var $url;
	var $username;
	var $password;
	var $merchant_code;
	var $merchant_number;
	var $terminal;

	var $product_code;
	var $bill_number;
	var $amount;
	var $fee_amount;
	var $bit61;
	var $mc_nohp;
	var $trax_id;
	var $transaction_code;
	var $time_stamp;

	var $signature;
	var $response;

	function __construct(){
		/*
		 * informasi account default
		 */
		$this->url = "https://www.mcmobilecash.com/devofc/FinChannelServices/routeX2.php?wsdl";
		$this->username = 'devtopaz';
		$this->password = '251fab7bf30b';
		$this->merchant_code = 'FNL111';
		$this->terminal = '006012';
		$this->merchant_number = '+62000000905';
	}

	/*
	 * method generate fractal time
	 */
	private function udate($format, $utimestamp = null)
	{
		if (is_null($utimestamp))
		$utimestamp = microtime(true);

		$timestamp = floor($utimestamp);
		$milliseconds = round(($utimestamp - $timestamp) * 1000000);

		return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
	}

	/*
	 * signature for billpaymentingquiry transaction
	 */
	private function signature_billpaymentinquery(){
		$signature = sha1("$this->username".md5("$this->password")."$this->product_code"."$this->merchant_code"."$this->terminal"."$this->merchant_number"."$this->transaction_code"."$this->bill_number"."$this->bit61"."$this->trax_id"."$this->time_stamp");

		return $signature;
	}

	/*
	 * signature for billpaymentpayment transaction
	 */
	private function signature_billpaymentpayment(){
		$signature = sha1("$this->username".md5("$this->password")."$this->product_code"."$this->merchant_code"."$this->terminal"."$this->merchant_number"."$this->transaction_code"."$this->bill_number"."$this->amount"."$this->fee_amount"."$this->bit61"."$this->trax_id"."$this->time_stamp");

		return $signature;
	}

	/*
	 * signature for billpaymentcheckstatus transaction
	 */
	private function signature_billpaymentstatus(){
		//$signature = sha1("$this->username".md5("$this->password")."$this->product_code"."$this->merchant_code"."$this->terminal"."$this->merchant_number"."$this->transaction_code"."$this->bill_number"."$this->trax_id"."$this->time_stamp");
		$signature = sha1("$this->username".md5("$this->password")."$this->product_code"."$this->merchant_code"."$this->terminal"."$this->merchant_number"."$this->transaction_code"."$this->bill_number"."$this->amount"."$this->fee_amount"."$this->bit61"."$this->trax_id"."$this->time_stamp");

		return $signature;
	}

	/*
	 * organized data for send
	 */
	private function data_send(){
		//generate signature by transaction code;
		$this->time_stamp = $this->udate('d-m-Y H:i:s:u');

		switch($this->transaction_code){
			case 38:
				$this->trax_id = substr(md5(uniqid(mt_rand(), true)), 0, 7);
				$this->signature = $this->signature_billpaymentinquery();
				break;
			case 50:
				$this->trax_id = substr(md5(uniqid(mt_rand(), true)), 0, 7);
				$this->signature = $this->signature_billpaymentpayment();
				break;
			case 77:
				$this->signature = $this->signature_billpaymentstatus();
				break;
		}

		if($this->transaction_code == 38 || $this->transaction_code == 50){
			$data_send = array('userName' => $this->username,
				'signature' => $this->signature,
				'productCode' => $this->product_code,
				'merchantCode' => $this->merchant_code,
				'terminal' => $this->terminal,
				'merchantNumber' => $this->merchant_number,
				'transactionType' => $this->transaction_code,
				'billNumber' => $this->bill_number,
				'amount' => $this->amount,
				'feeAmount' => $this->fee_amount,
				'bit61' => $this->bit61,
				'traxId' => $this->trax_id,
				'timeStamp' => $this->time_stamp,
				'mcNoHP' => $this->mc_nohp
			);
		} else if($this->transaction_code == 77){
			$data_send = array('userName' => $this->username,
				'signature' => $this->signature,
				'productCode' => $this->product_code,
				'merchantCode' => $this->merchant_code,
				'terminal' => $this->terminal,
				'merchantNumber' => $this->merchant_number,
				'transactionType' => $this->transaction_code,
				'billNumber' => $this->bill_number,
				'amount' => $this->amount,
				'feeAmount' => $this->fee_amount,
				'bit61' => $this->bit61,
				'traxId' => $this->trax_id,
				'timeStamp' => $this->time_stamp,
				'mcNoHP' => $this->mc_nohp
			);
		}

		return $data_send;
	}

	/*
	 * transaction bill inquiry
	 */
	function send_billinquiry(){
		$this->transaction_code = 38;
		$client = new SoapClient($this->url);
		$rx_data = $client->billpayment($this->data_send());

		foreach($rx_data as $key => $value){
			$this->response[$key] = $value;
		}
	}

	/*
	 * transaction bill payment
	 */
	function send_billpayment(){
		$this->transaction_code = 50;
		$client = new SoapClient($this->url);
		$rx_data = $client->billpayment($this->data_send());

		foreach($rx_data as $key => $value){
			$this->response[$key] = $value;
		}
	}

	/*
	 * transaction bill status
	 */
	function send_billstatus(){
		$this->transaction_code = 77;
		$client = new SoapClient($this->url);
		$rx_data = $client->billpayment($this->data_send());

		foreach($rx_data as $key => $value){
			$this->response[$key] = $value;
		}
	}
}

?>